/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.GrantRequest.perennialStipends;

import researchgrants.parts.GrantRequest.agency.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.LoggedData.HistoricallyLoggedData;
import researchgrants.parts.db.Db;

/**
 * A yearly value for PerennialStipends
 * This class has a directMoney and indirectMoney fields
 * @author DOStudent1
 */
public class Stipend {
    public static Stipend getByID(int perennialStipendsID, int yearIndex) {
        return(new Stipend(perennialStipendsID, yearIndex));
    }

    // the database keys to identify the stipend
    private int perennialStipendsID;
    private int yearIndex;
   
    // the logged data of it
    private HistoricallyLoggedData directMoney;
    private HistoricallyLoggedData indirectMoney;
    
    // private constructor
    private Stipend(int perennialStipendsID, int yearIndex) {
        this.perennialStipendsID=perennialStipendsID;
        this.yearIndex=yearIndex;
        syncDataFromDb();
    }

    // populates the loggedData from the database
    private void syncDataFromDb() {
        try {
            Connection conn = Db.openDbConnection();
            PreparedStatement prepareStatement = Db.createPreparedStatement(conn, "SELECT DirectMoneyRef, IndirectMoneyRef FROM tblStipends WHERE PerennialStipendsID=? AND YearIndex=? AND Active=1");
            prepareStatement.setInt(1, perennialStipendsID);
            prepareStatement.setInt(2, yearIndex);

            ResultSet rs = prepareStatement.executeQuery();
            Db.moveToFirstLine(rs);

            // populate the fields
            directMoney=HistoricallyLoggedData.getById((Integer) Db.getColumn(rs, 1));
            indirectMoney=HistoricallyLoggedData.getById((Integer) Db.getColumn(rs, 2));
            
            
            rs.close();
            prepareStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(Agency.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Agency.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    ////////////////////////////
    // Retreives the fields
    ////////////////////////////
    
    public int getYearIndex() {
        return(yearIndex);
    }
    
    public String getCurrentDirectMoney() {
        return(directMoney.value());
    }

    public String getCurrentIndirectMoney() {
        return(indirectMoney.value());
    }


    //////////////////////////////////////////////////
    // Retreives the raw fields - Used by the panels
    //////////////////////////////////////////////////
    
    HistoricallyLoggedData getLoggedDataDirectMoney() {
        return directMoney;
    }
    
    HistoricallyLoggedData getLoggedDataIndirectMoney() {
        return indirectMoney;
    }
    

}
